import java.util.Arrays;

public class Main {

//    int[] array = {1,2,3,4,5};

//    public static String myToString(int[] array){
//        String ret = "[";
//        for (int i = 0; i < array.length; i++) {
//            ret += array[i];
//            if (i < array.length-1) {
//                ret += ",";
//            }
//        }
//        ret += "]";
//        return ret;
//    }
    //拷贝数组
    public static int[] copyOf(int[] arr) {
        int[] ret = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            ret[i] = arr[i];
        }
        return ret;
    }
    //求数组平均值
    public static double avg(int[] arr) {
        double sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return sum / arr.length;
    }
    //查找数组位置；
    public static int find(int[] arr,int data) {
        for (int i = 0; i < arr.length; i++) {
            while (arr[i] == data) {
                return i;
            }
        }
        return -1;
    }

    public static int BinarySearch(int[] array1,int x) {
        int left = 0;
        int right = array1.length-1;
        while (left <= right) {
            int mid = (left+right) / 2;
            if (array1[mid] < x) {
               left = mid+1;
            } else if (array1[mid] > x) {
                right = mid-1;
            } else {
                return mid;
            }
        }
        return -1;
    }
    //冒泡排序
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length-1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length-1-i; j++) {
                while (array[j] > array[j+1]) {
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flg = true;
                }
            }
            if (flg == false) {
                break;//说明有序了
            }
        }
    }

    //数组逆序
    public static void resverse(int[] arr) {
        int left = 0;
        int right = arr.length-1;
        while (left < right) {
            int tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
            left++;
            right--;
        }
    }






    public static void main(String[] args) {


        //二维数组
        int[][] arr = {
                {1,2,3,4},
                {5,6,7,8},
                {9,10,11,12}
        };
        for (int row = 0; row < arr.length; row++) {
            for (int col = 0; col < arr[row].length; col++) {
                System.out.printf("%d\t", arr[row][col]);
            }
            System.out.println("");
        }


//        System.out.println(arr);


        int[] array2 = {1,14,4,2,55};
        int[] array1 = {1,2,13,4,15};

//        resverse(array2);
//        System.out.println(Arrays.toString(array2));
//        bubbleSort(array2);
//        System.out.println(Arrays.toString(array2));

//        System.out.println(BinarySearch(array1, 13));
//        System.out.println();
//        Arrays.sort(array1);
//
//        System.out.println(Arrays.toString(array1));
//        System.out.println(BinarySearch(array1,13));
//
//        System.out.println(Arrays.binarySearch(array1, 15));

//        int[] array = {1,2,3,4,5,6,7};
//        System.out.println(find(array,5));
//        System.out.println(find(array, 0));
//        System.out.println(find(array, 1));

//        System.out.println(avg(array));


//        System.out.println();
//        System.out.println(Arrays.toString(copyOf(array)));
//        System.out.println();

//        int[] array = {1,2,3,4,5,6,7};
//        System.out.println(myToString(array));
//        System.out.println();
//        int[] copy = new int[array.length*2];
//        int[] copy2 = Arrays.copyOfRange(array,1,17);//左闭右开
//        System.arraycopy(array,0,copy,0,array.length);
//        System.out.println();
//        System.out.println(Arrays.toString(copy2));
//        System.out.println();
//        System.out.println(Arrays.toString(copy));






//        System.out.println(s);
    }
}